package gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * Okno pro nastaveni jazyku
 * 
 * @author dove
 * 
 */
public class LanguageWin extends InternalFrame implements ActionListener {

	private static final long serialVersionUID = 1L;
	private String language;
	private JButton ok;
	private JButton canc;
	private JComboBox languageChoose;

	/**
	 * Nastaveni okna
	 * 
	 * @param name
	 *            jmeno okna
	 */
	public LanguageWin(String name) {
		super(name);
		JPanel pan = new JPanel();
		pan.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		c.insets = new Insets(10, 10, 10, 10);

		JLabel lanLab = new JLabel(messages.getString("ChooseL") + ":");
		c.gridx = 0;
		c.gridy = 1;
		pan.add(lanLab, c);

		String[] lanList = { messages.getString("English"),
				messages.getString("Czech") };
		languageChoose = new JComboBox(lanList);

		c.gridx = 1;
		c.gridy = 1;
		pan.add(languageChoose, c);
		c.gridy = 3;

		language = prefs.get("Language", "en");
		Locale currentLocale = new Locale(language);
		messages = ResourceBundle.getBundle("Lang", currentLocale);
		if (language.equals("en")) {
			languageChoose.setSelectedIndex(0);
		}
		if (language.equals("cs")) {
			languageChoose.setSelectedIndex(1);
		}

		logger.debug(language);

		c.gridx = 0;
		c.gridy = 4;
		ok = new JButton(messages.getString("Ok"));
		ok.addActionListener(this);
		pan.add(ok, c);

		c.gridx = 1;
		canc = new JButton(messages.getString("Cancel"));
		canc.addActionListener(this);
		pan.add(canc, c);

		this.add(pan);

		this.setLocation(30, 30);
		this.pack();
		this.setPreferredSize(new Dimension(400, 400));
	}

	public void actionPerformed(ActionEvent arg0) {
		Object action = arg0.getSource();
		if (action.equals(ok)) {
			if (languageChoose.getSelectedIndex() == 1) {
				if (prefs.get("Language", "en").equals("en")) {
					Desktop.infoMessage(messages.getString("restart_app"));
				}
				prefs.put("LanguageSetting", "cs");
			}
			if (languageChoose.getSelectedIndex() == 0) {
				if (prefs.get("Language", "en").equals("cs")) {
					Desktop.infoMessage(messages.getString("restart_app"));
				}
				prefs.put("LanguageSetting", "en");
			}

			logger.debug(languageChoose.getSelectedItem());
			this.setVisible(false);
		}
		if (action.equals(canc)) {
			this.setVisible(false);
		}
	}
}
